{extend name="property/public/insidePageBase" /}
{block name="titpropertyle"}待支付账单{/block}
{block name="body"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-inline">
                <label class="layui-form-label">关键词搜索</label>
                <div class="layui-input-inline">
                    <input type="text" name="keywords" autocomplete="off" class="layui-input" placeholder="请输入关键词搜索信息">
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
            </div>
        </form>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                {if $invalidAuth}<button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="invalid"> 作废账单 </button>{/if}
                {if $paymentAuth}<button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="payment"> 支付确认 </button>{/if}
            </div>
        </script>
        <table class="layui-hide" id="billBasicList" lay-filter="billBasicList"></table>
        <script type="text/html" id="tableBar">
            <div class="layui-btn-group">
                {if $paymentCustomAuth}<a class="layui-btn layui-btn-sm layui-bg-orange" lay-event="paymentCustomAdd">支付</a>
                {if $editAuth} <a class="layui-btn layui-btn-sm  layui-btn-normal" lay-event="edit">编辑</a>{/if}
                {if $detailAuth}<a class="layui-btn layui-btn-sm" lay-event="details">详情</a>{/if}
            </div>
        </script>
        <script type="text/html" id="bill_status_tpl">
            {{# if(d.status === 1) { }}<span class="layui-badge layui-bg-orange">待审核</span>{{# } }}
            {{# if(d.status === 2) { }}<span class="layui-badge layui-bg-blue">待支付</span>{{# } }}
            {{# if(d.status === 3) { }}<span class="layui-badge layui-bg-cyan">待确认</span>{{# } }}
            {{# if(d.status === 4) { }}<span class="layui-badge">已作废</span>{{# } }}
            {{# if(d.status === 5) { }}<span class="layui-badge layui-bg-black">已逾期</span>{{# } }}
            {{# if(d.status === 8) { }}<span class="layui-badge layui-bg-green">支付中</span>{{# } }}
            {{# if(d.status === 9) { }}<span class="layui-badge layui-bg-green">已完成</span>{{# } }}
        </script>
        <script type="text/html" id="start-end-date-tpl">
        {{# if(d.start_date) { }}{{d.start_date}} - {{d.end_date}}{{# } }}
    </script>
    </div>
</div>
{/block}
{block name="js"}
{__block__}
<script>
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery, form = layui.form, table = layui.table,laydate = layui.laydate;
        var tableId = 'billBasicList';
        laydate.render({
            elem: '#month-time',type: 'month'
        });
        table.render({
            elem: '#'+tableId,
            id:tableId,
            url: '{:url("propertyBillBasicStayPayCatalogList")}',
            toolbar: '#toolbar',
            cols: [[
                {type: "checkbox", width: 50, fixed:'left'},
                {field: 'month_time', width: 98, title: '年-月', sort: true},
                {field: 'cate_name', width: 128, title: '费用类别', sort: true},
                {field: 'property_name', width: 168, title: '物业信息', sort: true},
                {field: 'start_end_date_tpl', width: 187, title: '计费周期', sort: true, templet:'#start-end-date-tpl'},
                {field: 'status', width: 108, title: '支付状态',  sort: true, templet:'#bill_status_tpl'},
                {field: 'total_amount', width: 108, title: '总金额',  sort: true},
                {field: 'stay_pay_amount', width: 105, title: '待支付', templet:function(d) {
                        return d.stay_pay_amount + ' ¥';
                    },sort: true},
                {field: 'number', width: 168, title: '账单编号', sort: true}
                {if $editAuth || $delAuth}
                , {title: '操作', width: 168, toolbar: '#tableBar', fixed:'right'}
                {/if}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            height: 'full-80'
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {table.reload(tableId, {page: { curr: 1  }, where: data.field}, 'data'); return false; });
        //监听行双击事件
        table.on('toolbar('+tableId+')', function (obj) {
            if (obj.event === 'invalid') {  // 监听作废账单操作
                var checkStatus = table.checkStatus(obj.config.id),data = checkStatus.data;
                var data = checkStatus.data, id = [];
                for (elem in data) {
                    id.push(data[elem].id);
                }
                if (id.length === 0) {
                    return layer.msg('请选择要作废账单数据');
                }
                var url = '{:url("propertyBillCatalogInvalidSubmit")}';
                layer.prompt({title: '请输入作废事由' }, function(value, index, elem){
                    var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                    $.post(url, {ids: id,remark:value}, function (res) {
                        layer.close(index);
                        layer.close(loadAdd);
                        if (res.code === 0) {
                            table.reload(tableId); //数据刷新
                            layer.msg(res.msg, {icon: 6, offset: '15px'});
                        } else {
                            layer.msg(res.msg, {icon: 5, offset: '15px'});
                        }
                    });
                });
            }  else if (obj.event === 'payment') {  // 监听支付账单操作
                var checkStatus = table.checkStatus(obj.config.id),data = checkStatus.data;
                var data = checkStatus.data, id = [];
                for (elem in data) {
                    id.push(data[elem].id);
                }
                if (id.length === 0) {
                    return layer.msg('请选择确认支付账单');
                }
                var url = '{:url("propertyBillCatalogPaymentSubmit")}?id='+(id.join(','));
                var index = parent.layer.open({title: '己选的账单确认己支付', type: 2, shade: 0.2,maxmin:true, area: ['800px', '700px'], content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index],submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function(data){
                            var field = data.field;//获取提价的数据
                            var loadAdd = parent.layer.msg('提交中', {icon:16, time:0, shade:0.1, offset:'15px'});
                            $.post(url, field, function(res){
                                parent.layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    parent.layer.close(index); //关闭弹层
                                    parent.layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    parent.layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            })
                        })
                        submit.trigger('click');
                    }
                });
            }
        });

        table.on('rowDouble('+tableId+')', function(obj){
            var data = obj.data;
            var url = '{:url("propertyBillCatalogDetail")}?catalog_id=' + data.id;
            var index = parent.layer.open({title: data.cate_name +'  明细详情', type: 2, shade: 0.2, maxmin:true, shadeClose:true, move:false,area: ['1000px', '100%'],offset:  'rt', anim: 2, content: url , btn:false });
            return false;
        });
        //监听表格复选框选择
        table.on('checkbox('+tableId+')', function (obj) {
            console.log(obj)
        });

        table.on('tool('+tableId+')', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var url = '{:url("propertyBillCatalogEdit")}?id='+data.id;
                var index = layer.open({
                    title: data.number +' 账单编辑', type: 2,  shade: 0.2, maxmin:true, area:['800px', '90%'],content: url, btnAlign: 'c', btn: ['确定', '取消'],  yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    layer.close(index); //关闭弹层
                                    layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'details') {
                var url = '{:url("propertyBillCatalogDetail")}?catalog_id=' + data.id;
                var index = parent.layer.open({title: data.cate_name + ' 明细详情 ', type: 2,   shade: 0.2, maxmin: true, shadeClose: true,  move: false, area: ['1000px', '100%'],  offset: 'rt',anim: 2, content: url, btn: false});
                return false;
            } else if (obj.event === 'paymentCustomAdd') {
                var url = '{:url("propertyBillCatalogPaymentCustom")}?id='+data.id;
                var index = layer.open({
                    title: '账单支付 ' + data.cate_name + ' 账单', type: 2,  shade: 0.2, maxmin:true, area:['650px', '500px'],content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                layer.close(loadAdd);
                                if (res.code === 0) {
                                    layer.close(index); //关闭弹层
                                    layer.msg(res.msg, {icon: 6, offset: '15px',time: 1000}, function() {
                                        window.location.reload();
                                    });
                                } else {
                                    layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });
    });
</script>
{/block}